﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;

namespace kb_24_the_game
{
    internal class XML_IO
    {
        public List<ObstakelTest> readXML(string location)
        {
            FileStream fs = new FileStream(location, FileMode.Open);
            XmlDictionaryReader reader =
                XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
            DataContractSerializer ser = new DataContractSerializer(typeof(List<ObstakelTest>));

            List<ObstakelTest> obstakellijst =
                (List<ObstakelTest>)ser.ReadObject(reader, true);
            reader.Close();
            fs.Close();
            return obstakellijst;
        }

        public void writeXML(string location, List<Obstakel> oudeLijst)
        {
            List<ObstakelTest> obstakelLijst = new List<ObstakelTest>();
            ObstakelTest doorgeefobstakel;
            foreach (Obstakel o in oudeLijst)
            {
                doorgeefobstakel = new ObstakelTest();
                doorgeefobstakel.Location = o.textureLocation;
                doorgeefobstakel.position = o.position;
                doorgeefobstakel.type = o.type;
                doorgeefobstakel.valtimer = o.valTijd;
                obstakelLijst.Add(doorgeefobstakel);
                doorgeefobstakel = null;
            }
            DataContractSerializer serializer = new DataContractSerializer(typeof(List<ObstakelTest>));
            FileStream writer = new FileStream(location, FileMode.Create);
            serializer.WriteObject(writer, obstakelLijst);
            writer.Close();
        }
    }
}